package com.lanou;

import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by lanou on 18/1/25.
 */
@Controller
public class Skin {

    @Resource
    private JdbcTemplate jdbcTemplate;

    @RequestMapping("/pifu")
    public ModelAndView skin(ModelAndView modelAndView, HttpServletRequest request, String name) {
        HttpSession session = request.getSession();
        int gid = (int) session.getAttribute("gid");
        String sql = "select did ,dname , djieshao, money, durl from skin where gid = ? and fenlei = ?";
        RowMapper<Skin1> rowMapper = new BeanPropertyRowMapper<>(Skin1.class);
        List<Skin1> ss = jdbcTemplate.query(sql, rowMapper, gid, name);
        int a = ss.get(0).getDid();
        modelAndView.addObject("ss", ss);
        session.setAttribute("ss", ss);

        modelAndView.setViewName("shop");
        return modelAndView;
    }


    @RequestMapping("buy")
    @ResponseBody
    public String buy(int id, HttpServletRequest request) {


        HttpSession session = request.getSession();


        String sql = "select money from skin where did = ? ";
        RowMapper<Skin1> rowMapper = new BeanPropertyRowMapper<>(Skin1.class);
        Skin1 s1 = jdbcTemplate.queryForObject(sql, rowMapper, id);

        String uname = (String) session.getAttribute("uname");

        String sql1 = "select did , uname from record where uname = ? and did = ?";
        RowMapper<Record> rowMapper1 = new BeanPropertyRowMapper<>(Record.class);
        List<Record> lr = jdbcTemplate.query(sql1, rowMapper1, uname, id);


        int jiage = s1.getMoney();
        int money1 = (int) session.getAttribute("money");
        int money = money1 - jiage;

        if (lr.size() > 0) {
            return "bbb";
        } else if (money < 0) {

            session.setAttribute("money", money1);
            return "ccc";


        } else {
            jdbcTemplate.update("insert into record (did,uname,state)  values (?,?,1);", id, uname);

            session.setAttribute("money", money);
            return "aaa";
        }


    }


    @RequestMapping("show")

    public ModelAndView show(ModelAndView modelAndView, HttpServletRequest request) {
        HttpSession session = request.getSession();
        String uname = (String) session.getAttribute("uname");

        String sql = "select did from record where uname = ?";
        RowMapper<Record> rowMapper = new BeanPropertyRowMapper<>(Record.class);
        List<Record> lr = jdbcTemplate.query(sql, rowMapper, uname);
        List<Integer> ls = new ArrayList<>();

        List<Skin1> ls1 = new ArrayList<>();
        for (Record rr : lr) {
            int did = rr.getDid();
            ls.add(did);
        }

        String sql1 = "select dname,djieshao,money,durl from skin where did = ?";
        for (Integer ii : ls) {
            RowMapper<Skin1> rowMapper1 = new BeanPropertyRowMapper<>(Skin1.class);
            Skin1 ll = jdbcTemplate.queryForObject(sql1, rowMapper1, ii);
            ls1.add(ll);

        }


        modelAndView.addObject("ls1", ls1);
        modelAndView.setViewName("show");

        return modelAndView;
    }

    @RequestMapping("query")
    public ModelAndView query(ModelAndView modelAndView, HttpServletRequest request) {
        String condition = request.getParameter("condition");
        HttpSession session = request.getSession();
        String uname = (String) session.getAttribute("uname");

        String sql = "select * from skin where did in (select did from record where uname = ?) and fenlei = ? and state = 1;";
        RowMapper<Skin1> rowMapper = new BeanPropertyRowMapper<>(Skin1.class);
        List<Skin1> ls = jdbcTemplate.query(sql, rowMapper, uname, condition);


        modelAndView.addObject("ls", ls);
        modelAndView.addObject("condition", condition);
        modelAndView.setViewName("show1");


        return modelAndView;
    }


    @RequestMapping("state")
    public ModelAndView state(ModelAndView modelAndView, HttpServletRequest request) {
        HttpSession session = request.getSession();
        String uname = (String) session.getAttribute("uname");
        String sql = "update record set state = 2 where ";


        modelAndView.setViewName("show1");
        return modelAndView;
    }


}
